// 定义一个类
class Gretter {
  word: string
  constructor(msg: string) {
    this.word = msg
  }
  greet() {
    return 'hello' + this.word
  }
}
// 实例化Gretter
const gretter = new Gretter('world')
console.log(gretter.greet()); // 'helloworld'

// 使用接口去约束一个类
interface ClockInterface {
  currentTime: Date;
  getTime(format: string[]) : string
}
class Clock implements ClockInterface {
  // 接口只描述了类的公共部分
  a: string = ''
  currentTime: Date;
  constructor(time: number) {
    this.currentTime = new Date(time)
  }
  getTime(format: string[]) {
    const years = this.currentTime.getFullYear()
    const month = this.currentTime.getMonth()
    const date = this.currentTime.getDate()
    const hours = this.currentTime.getHours()
    const minutes = this.currentTime.getMinutes()
    const seconds = this.currentTime.getSeconds()
    return years + format[0] + month + format[0] + date + ' ' + hours + format[1] + minutes + format[1] + seconds
  }
}
const date: number = +new Date()
const clock = new Clock(date)
console.log(clock.getTime(['-', ':'])); // '2020-2-26 21:47:34'

